home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / trs141f.zip / NFMT.ZIP / OPTIONS.H < prev    next >
Text File  |  1992-07-20  |  3KB  |  137 lines

  1. /*
  2.  *    Option processing.
  3.  *
  4.  *    Conforms to intro(1) in System V.
  5.  *
  6.  *    Typical layout:
  7.  *
  8.  *        OPTIONS ("-b -t c -f file -w N +b file ...")
  9.  *            FLAG    ('b', flag)
  10.  *            CHAR    ('t', tab_ch)
  11.  *            STRING    ('f', filename)
  12.  *                f = fopen(filename, "r");
  13.  *            NUMBER    ('w', width)
  14.  *        MORE_OPTIONS('+')
  15.  *            FLAG    ('b', unflag)
  16.  *        ENDOPTS
  17.  *
  18.  * AUTHOR
  19.  *    Ross Paterson <rap@doc.ic.ac.uk>
  20.  *  All rights reserved.
  21.  */
  22.  
  23. char    *O_name, *O_usage;
  24.  
  25.  
  26. void usage ARGS((void));
  27.  
  28. void
  29. usage()
  30. {
  31.     /*
  32.      * Poor man's version of:
  33.      *
  34.      *    fprintf(stderr, "Usage: %s %s\n", O_name, O_usage);
  35.      */
  36.     write(2, "Usage: ", 7);
  37.     write(2, O_name, strlen(O_name));
  38.     write(2, " ", 1);
  39.     write(2, O_usage, strlen(O_usage));
  40.     write(2, "\n", 1);
  41.     exit(1);
  42. }
  43.  
  44. /*
  45.  *    An argument "-" is interpreted as a program argument and stops
  46.  *    option processing.
  47.  *
  48.  *    An argument "--" stops option processing and is discarded.
  49.  *
  50.  *    An option which takes an argument uses either the rest of the
  51.  *    the current argument, or, if at the end of an argument, the
  52.  *    next argument.
  53.  */
  54. #define OPTIONS(usage)                            \
  55.     O_usage = usage;                        \
  56.     O_name = argv[0];                        \
  57.     while (*++argv) {                        \
  58.         register int O_cont = 1;                \
  59.                                     \
  60.         if (**argv == '-') {                    \
  61.             if (argv[0][1] == '\0')                \
  62.                 break;                    \
  63.             argc--;                        \
  64.             if (argv[0][1] == '-' && argv[0][2] == '\0') {    \
  65.                 argv++;                    \
  66.                 break;                    \
  67.             }                        \
  68.             while (O_cont)                    \
  69.                 switch (*++*argv) {            \
  70.                 case '\0':                \
  71.                     O_cont = 0;
  72.  
  73. #define FLAG(c,flag)        break;                    \
  74.                 case c:                    \
  75.                     flag = 1;
  76.  
  77. #define NUMBER(c,num)        break;                    \
  78.                 case c:                    \
  79.                     if (*++*argv == '\0') {        \
  80.                         if (--argc == 0)    \
  81.                             usage();    \
  82.                         argv++;            \
  83.                     }                \
  84.                     num = atoi(*argv);        \
  85.                     O_cont = 0;
  86.  
  87. #define NUM_OPT(num)        break;                    \
  88.                 case '0': case '1': case '2': case '3':    \
  89.                 case '4': case '5': case '6': case '7':    \
  90.                 case '8': case '9':            \
  91.                     num = atoi(*argv);        \
  92.                     O_cont = 0;
  93.  
  94. #define STRING(c,str)        break;                    \
  95.                 case c:                    \
  96.                     if (*++*argv == '\0') {        \
  97.                         if (--argc == 0)    \
  98.                             usage();    \
  99.                         argv++;            \
  100.                     }                \
  101.                     str = *argv;            \
  102.                     O_cont = 0;
  103.  
  104. #define CHAR(c,ch)        break;                    \
  105.                 case c:                    \
  106.                     if (*++*argv == '\0') {        \
  107.                         if (--argc == 0)    \
  108.                             usage();    \
  109.                         argv++;            \
  110.                     }                \
  111.                     ch = **argv;            \
  112.                     O_cont = 0;
  113.  
  114. #define MORE_OPTIONS(c)        break;                    \
  115.                 default:                \
  116.                     usage();            \
  117.                 }                    \
  118.         }                            \
  119.         else if (**argv == (c)) {                \
  120.             if (argv[0][1] == '\0')                \
  121.                 break;                    \
  122.             argc--;                        \
  123.             while (O_cont)                    \
  124.                 switch (*++*argv) {            \
  125.                 case '\0':                \
  126.                     O_cont = 0;
  127.  
  128. #define ENDOPTS            break;                    \
  129.                 default:                \
  130.                     usage();            \
  131.                 }                    \
  132.         }                            \
  133.         else                            \
  134.             break;                        \
  135.     }                                \
  136.     *--argv = O_name;
  137.